<?php
class HkpostContract {
    var $date;
    var $location;
    var $content;
}

class Uuch_Track_HkpostAPI{
    public $url;
    public $body;
    public function __construct($url) {
        $this->url = $url;
        $client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($this->url);
        $response = $client->request();
        $this->body = $response->getBody();
    }
    
    public function query(){
        error_reporting(NULL);
        ini_set('display_errors','Off');
        $seletpreg = '%(<br\s*\/>\s*<table class="detail">|<p><span class="textNormalBlack">)\s*(.*?)(<\/table>|<br\s*/><br\s*/>)%si';
        $match = array();        
        preg_match_all($seletpreg, $this->body, $match);
        $arr = array();
        if(preg_match("/textNormalBlack/si",$match[0][0],$arr)){
            return $match[0][0];
        }else{
            $resultArray = $this->getArray($match[0][0]);
            unset($resultArray[0]);
            $resultList = array();        
            foreach ($resultArray as $value) {
                $model = new HkpostContract();
                $model->date = trim($value[0]);
                $model->location = trim($value[1]);
                $model->content = trim($value[2]);
                array_push($resultList, $model);
            }
            return $resultList;
        }        
    }
    
    private function getArray($table) {
        $table = preg_replace("'<br\s*\/>\s*'si", "", $table);
        $table = preg_replace("'<table[^>]*?>'si", "", $table);
        $table = preg_replace("'<tr[^>]*?>'si", "", $table);
        $table = preg_replace("'<td[^>]*?>'si", "", $table);
        $table = str_replace("</tr>", "{tr}", $table);
        $table = str_replace("</td>", "{td}", $table);
        //去掉 HTML 标记
        $table = preg_replace("'<[\/\!]*?[^<>]*?>'si", "", $table);
        //去掉空白字符 
        $table = preg_replace("'([\r\n])[\s]+'", "", $table);
        // $table = str_replace(" ", "", $table);
        // $table = str_replace(" ", "", $table);
        $table = explode('{tr}', $table);
        array_pop($table);
        foreach ($table as $key => $tr) {
            $td = explode('{td}', $tr);
            array_pop($td);
            $td_array[] = $td;
        }
        return $td_array;
    }
    
}










?>
